package com.pig.service.impl;

import com.pig.annotation.RequiredLog;
import com.pig.dao.UserMapper;
import com.pig.pojo.user.User;
import com.pig.service.UserService;
import com.pig.util.SysResultLayui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    /**分页查询用户信息
     * @return*/
    @RequiredLog(operation = "分页查询用户信息")
    @Override
    public SysResultLayui findUserByPage(int page, int limit) {
        int startIndex = (page-1)*limit;
        int count = userMapper.selectCount(null);
        List<User> itemList = userMapper.findUserByPage(startIndex, limit);
        return new SysResultLayui(200,"OK",count,itemList);
    }
    /**添加用户信息*/
    @Override
    @RequiredLog(operation = "添加用户信息")
    @Transactional  //开启事务的控制
    public void saveUsername(User user) {
        String password = user.getPassword();
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(pwd);
        user.setStatus(1L);
        user.setModifiedTime(new Date());
        user.setCreatedTime(new Date());
        userMapper.insertUser(user);
    }
    /**查看数据库是否有相同用户名*/
    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }
    /**修改用户信息*/
    @Override
    @RequiredLog(operation = "修改用户信息")
    @Transactional  //开启事务的控制
    public void updateUsers(User user) {
        String password = user.getPassword();
        System.out.println(password);
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(pwd);
        user.setModifiedTime(new Date());
        user.setCreatedTime(new Date());
        user.setStatus(1L);
        System.out.println(user.getId());
        userMapper.updateById(user);
    }

    /**删除用户信息*/
    @Override
    @RequiredLog(operation = "删除用户信息")
    public void deleteUser(Long id) {
        userMapper.deleteById(id);
    }
    /**修改密码*/
    @Override
    public SysResultLayui updatepassword(Integer id, String oldpass, String newpass) {

        User user = new User();
        try{
            user = userMapper.findUserById(id);
            oldpass = DigestUtils.md5DigestAsHex(oldpass.getBytes());
            if(user.getPassword().equals(oldpass)){
                newpass = DigestUtils.md5DigestAsHex(newpass.getBytes());
                userMapper.update(id);
            }else {
                return SysResultLayui.fail();
            }
        }catch (Exception e){
            return SysResultLayui.fail();
        }
        return SysResultLayui.success();
    }
    /**根据用户名查询用户信息
     * @return*/
    @Override
    public SysResultLayui findUserByUserName(String username) {

        return userMapper.findUserByUserName(username);
    }
    /**根据id改变用户状态*/
    @Override
    public SysResultLayui<User> updateUserStatusById(int id, int status) {
        userMapper.updateUserStatusById(id,status);
        return SysResultLayui.success();
    }


}
